package com.eastfair.venuebooking.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
//import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.eastfair.boot.service.SuperServiceImpl;
import com.eastfair.constant.BusinessConstant;
import com.eastfair.core.base.R;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.venuebooking.dao.PriceListSpaceMapper;
import com.eastfair.venuebooking.dto.PriceListSpaceQuery;
import com.eastfair.venuebooking.entity.PriceListSpace;
import com.eastfair.venuebooking.service.PriceListSpaceService;
import com.eastfair.venuebooking.vo.PriceListSpaceVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 业务实现类
 * 价目表场地关联
 * </p>
 *
 * @author linan
 * @date 2022-06-08
 */
@Slf4j
@Service
//@DS("#thread.tenant")
public class PriceListSpaceServiceImpl extends SuperServiceImpl<PriceListSpaceMapper, PriceListSpace> implements PriceListSpaceService {

    @Resource
    private PriceListSpaceMapper priceListSpaceMapper;

    /**
     * 处理逻辑删除相关处理
     *
     * @param modelList 实体
     * @return 是否成功
     */
    @Override
    protected R<Boolean> handlerRemoveByIdsOfLogic(Collection<PriceListSpace> modelList) {
        modelList.forEach(s -> s.setIsDeleted(BusinessConstant.YES));
        return R.successDef();
    }

    @Override
    protected R<Boolean> handlerSave(PriceListSpace model) {
        model.setProjectId(ContextUtil.getProjectId());
        model.setTenantId(ContextUtil.getTenantId());
        model.setSubsystemId(ContextUtil.getSubSystemId());
        return R.successDef();
    }

    @Override
    public List<PriceListSpaceVO> listPriceListSpaces(PriceListSpaceQuery query) {
        log.info("selectPriceListSpaceList - 查询价目场地列表, query={}", query);

        QueryWrapper<PriceListSpace> queryWrapper = createPriceListSpaceQueryWrapper(query);
        List<PriceListSpaceVO> voList = priceListSpaceMapper.listPriceListSpaces(queryWrapper);
        if (voList == null || voList.isEmpty()) {
            return voList;
        }
        // 通过场地id筛选
        List<Long> excludeSpaceIds = query.getExcludeSpaceIds();
        if (excludeSpaceIds == null || excludeSpaceIds.isEmpty()) {
            return voList;
        }
        return voList
                .stream()
                .filter(priceListSpaceVO -> !excludeSpaceIds.contains(priceListSpaceVO.getSpaceId()))
                .collect(Collectors.toList());
    }

    @Override
    public List<String> listTags(List<Long> priceListIds) {
        log.info("listTags - 查询场地展厅列表, priceListIds={}", priceListIds);
        List<String> tags = priceListSpaceMapper.listTags(priceListIds);
        if (tags == null || tags.isEmpty()) {
            return Collections.emptyList();
        }
        List<String> rawTags = new ArrayList<>(16);
        tags.forEach(tag -> CollectionUtil.addAll(rawTags, StrUtil.split(tag, ",")));
        return rawTags;
    }

    /**
     * 添加价目表跟展厅信息
     *
     * @param priceListSpaceList
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean insertBatchPriceList(List<PriceListSpace> priceListSpaceList) {
        for (PriceListSpace priceListSpace : priceListSpaceList) {
            priceListSpace.setSubsystemId(ContextUtil.getSubSystemId());
            priceListSpace.setProjectId(ContextUtil.getProjectId());
        }
        return this.saveBatch(priceListSpaceList);
    }

    /**
     * 创建价目场地查询.
     *
     * @param query query
     * @return QueryWrapper
     */
    private QueryWrapper<PriceListSpace> createPriceListSpaceQueryWrapper(PriceListSpaceQuery query) {
        QueryWrapper<PriceListSpace> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .eq("pl.is_deleted", BusinessConstant.NO)
                .and(StrUtil.isNotBlank(query.getKeyword()), priceListQueryWrapper -> priceListQueryWrapper
                        .like("sp.space_name", query.getKeyword())
                        .or()
                        .like("sp.tag", query.getKeyword()))
                .eq(query.getPriceListId() != null, "pl.id", query.getPriceListId())
                .eq(query.getSpaceCategoryId() != null, "spc.id", query.getSpaceCategoryId())
                .in(query.getPriceListSpaceIds() != null && !query.getPriceListSpaceIds().isEmpty(),
                        "pls.id", query.getPriceListSpaceIds())
                .orderByDesc("sp.create_time");
        List<String> tags = query.getTags();
        if (tags != null && !tags.isEmpty()) {
            tags.forEach(tag -> queryWrapper.like("sp.tag", tag));
        }
        return queryWrapper;
    }
}
